package com.sort;

import java.util.Random;

public class Test_2 {
    public static void main(String[] args) {
        int[] arr = new int[100000000];
        Random random = new Random();
        for (int i = 0; i < arr.length; i++) {
            arr[i] = random.nextInt(1000) + 1;
        }
            long s = System.currentTimeMillis();
         B.mergesort(arr, 0, arr.length - 1);
            long e = System.currentTimeMillis();
            System.out.println(e - s);

    }
}

class B{
    public static void mergesort(int[]arr,int left,int right ){
        if(left==right){
            return;
        }
        int mid=(left+right)/2;
        mergesort(arr,left,mid);
        mergesort(arr,mid+1,right);
        merge(arr,left,right,mid);
    }
    public static void merge(int[] arr,int left,int right,int mid){
        int s1=left;
        int e1=mid;
        int s2=mid+1;
        int e2=right;
        //
        int k=0;
        int[]temarr=new int[right-left+1];
        while(s1<e1&&s2<e2){
            if(arr[s1]<=arr[s2]){
                temarr[k++]=arr[s1++];
            }
            else{
                temarr[k++]=arr[s2++];
            }
        }
        while(s1<e1){
            temarr[k++]=arr[s1++];
        }
        while(s2<e2){
            temarr[k++]=arr[s2++];
        }
        //
        for(int i=0;i< temarr.length;i++){
            arr[i+left]=temarr[i];
        }
    }
}
